// pages/second/second.js
const DB = wx.cloud.database().collection("User")
var app = getApp()
Page({


  /**
   * 页面的初始数据
   */

  data: {
    status: ["未参加", "已参加"],
    statID: 0,
    terms: ["2019", "2020", "2021", "2022"],
    tID: 0,//学期编号
    academy: ["信息学院"],
    aID: 0,//学院编号
    Temp_Events: [],
    events:
      [
      ]
  },
  ChangeEvents: function () {
    var i = []
    var j = []
    var k = []
    let db = wx.cloud.database()
    db.collection('SecondEvent').get().then(res => {
      j = res.data
      DB.where({
        account: app.globalData.currentAccount
      }).get().then(res1 => {
        i = res1.data[0].AttendEvenID
        for (let m = 0; m < j.length; m++) {
          for (let n = 0; n < i.length; n++) {
            if (j[m].evenID === i[n]) {
              j[m].attend = true
              break
            }
          }
        }
        var z = 0
        if (this.data.status[this.data.statID] == "已参加") {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === true && j[m].tID - this.data.tID === 0) {
              k[z] = j[m]
              z++
            }
          }
        }
        else {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === false && j[m].tID - this.data.tID === 0) {
              k[z] = j[m]
              z++
            }
          }
        }
        this.setData({
          events: k
        })
      })
    })
  },

  StatChange: function (e) {
    console.log("stat_select", e.detail.value)
    this.setData(
      {
        statID: e.detail.value
      }
    )
    this.ChangeEvents()
  },
  TermChange: function (e) {
    console.log("term_select", e.detail.value)
    this.setData(
      {
        tID: e.detail.value
      }
    )
    this.ChangeEvents()
  },
  AcadeChange: function (e) {
    console.log("aca_select", e.detail.value)
    this.setData(
      {
        aID: e.detail.value
      }
    )
    this.ChangeEvents()
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var i = []
    var j = []
    var k = []
    let db = wx.cloud.database()
    db.collection('SecondEvent').get().then(res => {
      j = res.data
      DB.where({
        account: app.globalData.currentAccount
      }).get().then(res1 => {
        i = res1.data[0].AttendEvenID
        for (let m = 0; m < j.length; m++) {
          for (let n = 0; n < i.length; n++) {
            if (j[m].evenID === i[n]) {
              j[m].attend = true
              break
            }
          }
        }
        var z = 0
        if (this.data.status[this.data.statID] == "已参加") {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === true && j[m].tID === this.data.tID) {
              k[z] = j[m]
              z++
            }
          }
        }
        else {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === false && j[m].tID === this.data.tID) {
              k[z] = j[m]
              z++
            }
          }
        }
        this.setData({
          events: k
        })
      })
    })
  },

  onPostTap: function (event) {

    var id = event.currentTarget.dataset.detailid;
    var events = this.data.events
    for (var i = 0; i < events.length; i++) {
      if (events[i]._id === id) {
        if (events[i].attend == false) {
          events[i].attend = true
          var a = []
          var b
          let db = wx.cloud.database()
          db.collection('User').where({
            account: app.globalData.currentAccount
          }).get().then(res => {
            a = res.data[0].AttendEvenID
            b = res.data[0]._id
            a.push(events[i].evenID)
            console.log(a)
            DB.doc(b).update({
              data: {
                AttendEvenID: a
              },
            })
          })
          this.setData({
            events: events
          })
        }
        else {
          events[i].attend = false
          var a = []
          var b
          var c = []
          let db = wx.cloud.database()
          db.collection('User').where({
            account: app.globalData.currentAccount
          }).get().then(res => {
            a = res.data[0].AttendEvenID
            b = res.data[0]._id
            for (var j = 0; j < a.length; j++) {
              console.log(a[j])
              if (a[j] !== events[i].evenID) {
                c.push(a[j])
                console.log(a[j])
              }
            }
            DB.doc(b).update({
              data: {
                AttendEvenID: c
              },
            })
          })


          this.setData({
            events: events
          })
        }
        /**for(let n=0;n<i.length;n++){
         
          j[m].attend=true
            break
          }
        }*/

        break
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.ChangeEvents(),
      wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})